
package com.primecodegen.common.MainProject;

//import com.primecodegen.common.MainProjectGroup.MainProjectGroupDAO;
//import com.primecodegen.common.MainProjectGroup.MainProjectGroupTable;
import com.primecodegen.util.*;
import com.primecodegen.common.base.BaseBean;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.logging.*;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import javax.servlet.http.HttpSession;

@ManagedBean(name = "mainProjectBean")
@RequestScoped
public class MainProjectBean extends BaseBean implements Serializable {

    // *********** Local Variables/Objects ***********
    private static final Logger logger = Logger.getLogger(MainProjectBean.class.getName());
    private int count;
    private int mainProjectID;
    private String excludedClmn;
    private MainProjectTable selectedMainProject;
    HttpSession session;
    private MainProjectDAO mainProjectDAO;
    private ArrayList<MainProjectTable> mainProjectList;
    private MainProjectTable mainProjectSrchCrit;
    private MainProjectTable mainProjectUpdate;
    private MainProjectTable mainProjectTable;

    FacesContext context;
    ColToOrderBy colToOrderBy;

    // *********** Constructors ***********
    public MainProjectBean() {
        logger.info(" Constructor MainProjectBean()");
        excludedClmn="6";
        context = getContext();
        session = getSession();
        mainProjectDAO = new MainProjectDAO();
        mainProjectList = new ArrayList<MainProjectTable>();
        mainProjectTable = new MainProjectTable();
        mainProjectSrchCrit = (MainProjectTable) session.getAttribute("sessionMainProjectSrchCrit");
        mainProjectUpdate = (MainProjectTable) session.getAttribute("mainProjectUpdateSess");
        colToOrderBy = (ColToOrderBy) session.getAttribute("sessionColToOrderBy");

        if (mainProjectSrchCrit == null) {
            mainProjectSrchCrit = new MainProjectTable();
        }

        if (mainProjectUpdate == null) {
            mainProjectUpdate = new MainProjectTable();
        }



        fillMainProjectList();
    }


    // *********** Setters & Getters ***********
    public String getExcludedClmn() {
        return excludedClmn;
    }

    public void setExcludedClmn(String excludedClmn) {
        this.excludedClmn = excludedClmn;
    }

    public MainProjectTable getMainProjectUpdate() {
        mainProjectUpdate = (MainProjectTable) session.getAttribute("mainProjectUpdateSess");
         if (mainProjectUpdate == null) {
            mainProjectUpdate = new MainProjectTable();
        }
        return mainProjectUpdate;
    }

    public void setMainProjectUpdate(MainProjectTable mainProjectUpdate) {
        session.setAttribute("mainProjectUpdateSess", mainProjectUpdate);
        this.mainProjectUpdate = mainProjectUpdate;
    }



    public MainProjectBean(MainProjectTable mainProjectTable) {
        logger.info("Constructor MainProjectBean( MainProjectTable mainProjectTable )");
        this.mainProjectTable = mainProjectTable;
    }

    public MainProjectTable getMainProjectTable() {
        return mainProjectTable;
    }

    public void setMainProjectTable(MainProjectTable mainProjectTable) {
        this.mainProjectTable = mainProjectTable;
    }

    public MainProjectTable getMainProjectSrchCrit() {
        return mainProjectSrchCrit;
    }

    public void setMainProjectSrchCrit(MainProjectTable mainProjectSrchCrit) {
        this.mainProjectSrchCrit = mainProjectSrchCrit;
    }

    public MainProjectTable getSelectedMainProject() {
        return selectedMainProject;

    }

    public void setSelectedMainProject(MainProjectTable selectedMainProject) {
        this.selectedMainProject = selectedMainProject;
    }

    public ArrayList<MainProjectTable> getMainProjectList() {
        return mainProjectList;
    }


    // *********** Actions ***********
    public void doFilter() {
        logger.log(Level.INFO, "Starting: public void doFilter () in MainProjectBean");
        try {
            mainProjectSrchCrit.setFilter(true);
            this.mainProjectList = mainProjectDAO.getByCriterionOrderedBy(mainProjectSrchCrit, null);

            session.setAttribute("sessionMainProjectSrchCrit", mainProjectSrchCrit);
            fillMainProjectList();
            logger.log(Level.INFO, "Finishing: public void doFilter () in MainProjectBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in doFilter() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }

        public void clearFilter(){
        try {
            mainProjectSrchCrit = null;
            session.setAttribute("sessionMainProjectSrchCrit",mainProjectSrchCrit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sortByID() {

        logger.log(Level.INFO, "public void fillMainProjectList ()");

        try {

            if (mainProjectSrchCrit != null && colToOrderBy != null) {
                this.mainProjectList = mainProjectDAO.getByCriterionOrderedBy(mainProjectSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.mainProjectList = mainProjectDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (mainProjectSrchCrit != null) {
                this.mainProjectList = mainProjectDAO.getByCriterionOrderedBy(mainProjectSrchCrit, null);
            } else {
                this.mainProjectList = mainProjectDAO.getByCriterionOrderedBy(null, null);
            }




        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillMainProjectList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }

    public void update() {
        try {
            int parID = new Integer(session.getAttribute("mainProjectIDSess").toString());
            mainProjectUpdate.setID(parID);
            mainProjectDAO.updateByID(mainProjectUpdate);

        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured interface the update action, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in Update(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void Insert() {
        logger.info("public void Insert()");
        try {

            int isInserted = -1;


            isInserted = mainProjectDAO.insert(mainProjectTable);

            if (isInserted != -1) {
                getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved Successfully", "MainProject Added"));
                fillMainProjectList();
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", "error shit"));
                logger.log(Level.WARNING, "Exception occured in Insert(). Error Message: isInserted != -1 Probably exception occure in mainProjectDAO.insert( mainProjectTable )");

            }

        } catch (Exception e) {

            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void delete() {
        try {

            int parID = new Integer(session.getAttribute("mainProjectIDSess").toString());



            mainProjectDAO.deleteByID(parID);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "MainProject Deleted"));
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure", "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
        }
    }

    public void setIDinSession() {
        try {
            Map<String, String> params = context.getExternalContext().getRequestParameterMap();
            int parID = new Integer(params.get("mainProjectID"));
            mainProjectUpdate = (MainProjectTable) mainProjectDAO.getByID(parID);
            session.setAttribute("mainProjectUpdateSess", mainProjectUpdate);
            session.setAttribute("mainProjectIDSess", parID);
        } catch (Exception ex) {
            Logger.getLogger(MainProjectBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fillMainProjectList() {
        logger.log(Level.INFO, "Starting: public void fillMainProjectList () in MainProjectBean");
        try {

            if ((mainProjectSrchCrit != null && mainProjectSrchCrit.isFilter()) && colToOrderBy != null) {
                this.mainProjectList = mainProjectDAO.getByCriterionOrderedBy(mainProjectSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.mainProjectList = mainProjectDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (mainProjectSrchCrit != null && mainProjectSrchCrit.isFilter()) {
                this.mainProjectList = mainProjectDAO.getByCriterionOrderedBy(mainProjectSrchCrit, null);
            } else {
                this.mainProjectList = mainProjectDAO.getByCriterionOrderedBy(null, null);
            }



            logger.log(Level.INFO, "finishing: public void fillMainProjectList () in MainProjectBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillMainProjectList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }
 

}

